package com.ndbg.demo.handler;


import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ExceptionHandler {
    /**
     * 处理自定义的业务异常
     */
//    @ExceptionHandler(value = MyException.class)
//    public R<String> bizExceptionHandler(MyException e) {
//        log.error("发生业务异常！ msg: -> ", e);
//        return R.failed(e.getMessage());
//    }

    @org.springframework.web.bind.annotation.ExceptionHandler(value = NullPointerException.class)
    public ModelAndView nullExceptionHandler(HttpServletRequest request, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        e.printStackTrace();
        System.out.println("空指针异常" + e.getMessage());
        modelAndView.addObject("message", e);
        modelAndView.setViewName("空指针异常");
        return modelAndView;
    }
}
